let start = 1;

let pageSize = 10;
laydate.render({
    elem: '.date'
});

laydate.render({
    elem: '#product-hours-report-time',
    type: 'month',
    value: getTime()
});
laydate.render({
    elem: '#project-hours-report-time',
    type: 'month',
    value: getTime()
});
let other = false;
laydate.render({
    elem: '#manage-hours-report-time',
    type: 'month',
    value: getTime(),
    done: function (value) {
        let jobNumber = null
        if (other) {
            jobNumber = $('#other-jobNumber').val();
        }
        get_cur_month_mwh(start, jobNumber, value);
    }
});
laydate.render({
    elem: '#recordDate'
});
laydate.render({
    elem: '#stat-time',
    type: 'month'
});

laydate.render({
    elem: '#project-stat-time',
    type: 'month'
});

laydate.render({
    elem: '#manage-stat-time',
    type: 'month'
});


$(function () {
    get_production_working_hours(start, pageSize);
    get_all_project_number();
    get_all_workOrderNumber();
});

function get_all_workOrderNumber() {
    $.get({
        url: "/plan/getAllWorkOrderNumber",
        success: function (result) {
            if (result.status === 200) {
                package_menu(result.data, $('.workOrderNumber-menu'))
            }
        }
    })
}

function get_user_part_info() {
    $.get({
        url: '/admin/getUserPartInfo',
        success: function (result) {
            if (result.status === 200) {
                let data = result.data;
                $('#name').val(data.name);
                $('#jobNumber').val(data.username);
                $('#position').val(data.role);
            }
        }
    })
}

function get_all_project_number() {
    $.get({
        url: '/plan/getAllProjectNumber',
        success: function (result) {
            if (result.status === 200) {
                let menu = $('.m-projectNumber-menu');
                package_menu_plus(result.data, menu);
                menu.append("<div class='item' data-value='cancel'><span>不选择</span></div>");
            }
        }
    })
}

function get_production_working_hours(start, pageSize) {
    $.get({
        url: '/plan/obtainProductionTimeInformation',
        data: {
            "pageNum": start,
            "pageSize": pageSize
        },
        success: function (result) {
            if (result.status === 200) {
                pack_production_hours_info(result.data.list, $('#view1-table tbody'));
                renderTfoot(pageSize, result.data, 'get_production_working_hours', "1", start);
            }
        }
    })
}

function get_project_working_hours_info(start, pageSize) {
    $.get({
        url: '/plan/obtainTimeInformationOfAllProjects',
        data: {
            "pageNum": start,
            "pageSize": pageSize
        },
        success: function (result) {
            if (result.status === 200) {
                pack_project_hours_info(result.data.list, $('#view2-table tbody'));
                renderTfoot(pageSize, result.data, 'get_project_working_hours_info', "1", start);
            }
        }
    })
}

function get_manage_working_hours_info(start, pageSize) {
    $.get({
        url: '/plan/getPersonalAllManageWorkingHoursInfo/' + start + "/" + pageSize,
        success: function (result) {
            if (result.status === 200) {
                page_management_hours_info(result.data.list, $('#view3-table tbody'));
                renderTfoot(pageSize, result.data, 'get_manage_working_hours_info', "1", start);
            }
        }
    })
}

function get_manage_working_hours_info_by_projectNumber(start, pageSize, projectNumber) {
    let jobNumber = $('#other-jobNumber').val();
    if (jobNumber.length === 0) {
        jobNumber = null;
    }
    $.get({
        url: '/plan/getPersonalAllManageWorkingHoursInfoByProjectNumber/' + projectNumber + "/" + jobNumber + "/" + start + "/" + pageSize,
        success: function (result) {
            if (result.status === 200) {
                page_management_hours_info(result.data.list, $('#view3-table tbody'));
                renderTfoot(pageSize, result.data, 'get_manage_working_hours_info_by_projectNumber', projectNumber, start);
            }
        }
    })
}

function get_manage_working_hours_info_by_jobNumber(start, pageSize, jobNumber) {
    $.get({
        url: '/plan/getPersonalAllManageWorkingHoursInfoByJobNumber/' + jobNumber + "/" + start + "/" + pageSize,
        success: function (result) {
            if (result.status === 200) {
                page_management_hours_info(result.data.list, $('#view3-table tbody'));
                renderTfoot(pageSize, result.data, 'get_manage_working_hours_info_by_jobNumber', jobNumber, start);
            }
        }
    })
}

$('#other-jobNumber').change(function () {
    let jobNumber = $(this).val();
    if (jobNumber.length === 0) {
        return;
    }
    get_manage_working_hours_info_by_jobNumber(start, pageSize, jobNumber);
})

$('#zy-projectNumber').change(function () {
    let projectNumber = $(this).val();
    if (projectNumber.length === 0 || projectNumber === "cancel") {
        return;
    }
    get_manage_working_hours_info_by_projectNumber(start, pageSize, projectNumber);
})


function pack_project_hours_info(data, target) {
    if (data.length < 1) {
        let head = $('#view1-table thead');
        target.html("<tr><td colspan='" + head.children().children().length + "'><h2 style='font: italic 2em Georgia, serif;margin: 0 50px;'>no result</h2></td></tr>");
        return;
    }
    target.empty();
    $.each(data, function (index, item) {
        target.append(
            "<tr>" +
            "<td></td>" +
            "<td>" + item.projectNumber + "</td>" +
            "<td>" + item.projectName + "</td>" +
            "<td>" + item.projectDate + "</td>" +
            "<td>" + item.numberOfOperations + "</td>" +
            "<td>" + item.workingTime + "h</td>" +
            "<td>" + item.overTime + "h</td>" +
            "<td>" + item.totalWorkingTime + "h</td>" +
            "<td data-tooltip='" + item.remarks + "' data-position='top right'><div class='remarks' style='width: 100px!important;'>" + item.remarks + "</div></td>" +
            "<td>" + item.name + "</td>" +
            "<td><a class='del-project-hours' data-value='" + item.id + "'><i class='trash alternate outline icon'></i>删除</a></td>" +
            "</tr>"
        )
    })
}

function pack_production_hours_info(data, target) {
    if (data.length < 1) {
        let head = $('#view1-table thead');
        target.html("<tr><td colspan='" + head.children().children().length + "'><h2 style='font: italic 2em Georgia, serif;margin: 0 50px;'>no result</h2></td></tr>");
        return;
    }
    target.empty();
    $.each(data, function (index, item) {
        target.append(
            "<tr>" +
            "<td></td>" +
            "<td>" + item.workOrderNumber + "</td>" +
            "<td>" + item.productCode + "</td>" +
            "<td>" + item.productNumber + "</td>" +
            "<td>" + item.productionQuantity + "</td>" +
            "<td>" + item.actualOutput + "</td>" +
            "<td>" + item.workingHours + "h</td>" +
            "<td>" + item.overTime + "h</td>" +
            "<td>" + item.totalWorkingHours + "h</td>" +
            "<td data-tooltip='" + item.remarks + "' data-position='top right'><div class='remarks' style='width: 100px!important;'>" + item.remarks + "</div></td>" +
            "<td>" + item.recordDate + "</td>" +
            "<td>" + item.name + "</td>" +
            "<td><a class='del-production-hours' data-value='" + item.id + "'><i class='trash alternate outline icon'></i>删除</a></td>" +
            "</tr>"
        )
    })
}

function page_management_hours_info(data, target) {
    if (data.length < 1) {
        let head = $('#view1-table thead');
        target.html("<tr><td colspan='" + head.children().children().length + "'><h2 style='font: italic 2em Georgia, serif;margin: 0 50px;'>no result</h2></td></tr>");
        return;
    }
    target.empty();
    $.each(data, function (index, item) {
        target.append(
            "<tr>" +
            "<td></td>" +
            "<td>" + item.name + "</td>" +
            "<td>" + item.jobNumber + "</td>" +
            "<td>" + item.projectNumber + "</td>" +
            "<td>" + item.projectName + "</td>" +
            "<td>" + item.workingTime + "h</td>" +
            "<td>" + item.overTime + "h</td>" +
            "<td>" + item.totalWorkingTime + "h</td>" +
            "<td data-tooltip='" + item.remarks + "' data-position='top right'><div class='remarks' style='width: 100px!important;'>" + item.remarks + "</div></td>" +
            "<td>" + item.recordDate + "</td>" +
            "<td><a class='del-management-hours' data-value='" + item.id + "'><i class='trash alternate outline icon'></i>删除</a></td>" +
            "</tr>"
        )
    })
}

$(document).on('click', '.del-production-hours', function () {
    let _this = $(this);
    let id = _this.attr('data-value');
    $.get({
        url: '/plan/delProductionHours/' + id,
        success: function (result) {
            if (result.status === 200) {
                success(result.msg);
                _this.parent().parent().remove();
            }
        }
    })
})

$(document).on('click', '.del-project-hours', function () {
    let _this = $(this);
    let id = _this.attr('data-value');
    $.get({
        url: '/plan/delProjectHours/' + id,
        success: function (result) {
            if (result.status === 200) {
                success(result.msg);
                _this.parent().parent().remove();
            }
        }
    })
})

$(document).on('click', '.del-management-hours', function () {
    let _this = $(this);
    let id = _this.attr('data-value');
    $.get({
        url: '/plan/delManagementHours/' + id,
        success: function (result) {
            if (result.status === 200) {
                success(result.msg);
                _this.parent().parent().remove();
            }
        }
    })
})

$('#workOrderNumber').change(function () {
    let workOrderNumber = $(this).val();
    if (workOrderNumber.length === 0) {
        return
    }
    $.get({
        url: '/plan/getWorkingHoursInfo/' + workOrderNumber,
        success: function (result) {
            if (result.status === 200) {
                let data = result.data;
                $('#productNumber').val(data.productNumber);
                $('#productCode').val(data.productCode);
                $('#productionQuantity').val(data.productionQuantity);
            }
        }
    })
});

$('.search-work-hours').click(function () {
    search_work_hours(start, pageSize);
});

$('.search-project_info').click(function () {
    search_project_hours(start, pageSize);
})

function search_project_hours(start, pageSize) {
    let searchValue = $('#project-text').val().trim();
    $.get({
        url: '/plan/getProjectInfoByNumberOrName',
        data: {
            "searchValue": searchValue,
            "pageNum": start,
            "pageSize": pageSize
        },
        success: function (result) {
            pack_project_hours_info(result.data.list, $('#view2-table tbody'));
            renderTfoot(pageSize, result.data, 'search_project_hours', "1", start);
        }
    })
}

function search_work_hours(start, pageSize) {
    let workOrderNumber = $('#search-work').val();
    if (verify_work_order_number(workOrderNumber)) {
        failure("不合法工单号")
        return
    }
    $.get({
        url: '/plan/queryHoursByWorkNumber/' + workOrderNumber.trim() + "/" + start + "/" + pageSize,
        success: function (result) {
            if (result.status === 200) {
                pack_production_hours_info(result.data.list, $('#view1-table tbody'))
                renderTfoot(pageSize, result.data, 'search_work_hours', "1", start);
            }
        }
    })
}

$('.re1').click(function () {
    clear_re1()
});
$('.re2').click(function () {
    $('#no-production-hours-form').form('clear');
});
$('.re3').click(function () {
    clear_re3();
});
$('.re4').click(function () {
    $('#add-project-form').form('clear');
});

function clear_re3() {
    $('#manage-date').val("")
    $('#m-noWorkingTime').val("");
    $('#m-noOverTime').val("");
    $('#m-noTotalWorkingTime').val("");
    $('#no-m-remarks').val("");
}

function clear_re1() {
    $('#recordDate').val("");
    $('#actualOutput').val("");
    $('#workingHours').val("");
    $('#overTime').val("");
    $('#totalWorkingHours').val("");
    $('#remarks').val("");
}


$('#m-projectNumber').change(function () {
    let projectNumber = $(this).val();
    if (projectNumber.length === 0) {
        return;
    }
    $.get({
        url: '/plan/getProjectInfo/' + projectNumber,
        success: function (result) {
            if (result.status === 200) {
                $('#m-projectName').val(result.data);
            }
        }
    })
});
$('#projectNumber').change(function () {
    let projectNumber = $(this).val();
    if (projectNumber.length === 0) {
        return;
    }
    $.get({
        url: '/plan/getProjectInfoByProjectNumber/' + projectNumber,
        success: function (result) {
            if (result.status === 200) {
                $('#projectName').val(result.data);
            }
        }
    })
})

$('#add-project-form').form({
    inline: true,
    keyboardShortcuts: false,
    on: "submit",
    fields: {
        projectNumber: {
            identifier: "add-projectNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写项目编号"
                }
            ]
        },
        projectName: {
            identifier: "add-projectName",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写项目名称"
                }
            ]
        }
    },
    onSuccess: function () {
        add_project();
    }
})

$('#del-project-form').form({
    inline: true,
    keyboardShortcuts: false,
    on: "submit",
    fields: {
        projectNumber: {
            identifier: "del-projectNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "请选择要删除的项目编号"
                }
            ]
        }
    },
    onSuccess: function () {
        del_project();
    }
})

function add_project() {
    let projectNumber = $('#add-projectNumber').val().trim();
    let projectName = $('#add-projectName').val().trim();
    $.post({
        url: '/plan/addProject',
        data: {
            "projectNumber": projectNumber,
            "projectName": projectName
        },
        success: function (result) {
            if (result.status === 200) {
                success(result.msg);
                $('#add-project-form').form('clear');
            }
        }
    })
}

function del_project() {
    $.get({
        url: '/plan/deleteProject/' + $('#del-projectNumber').val(),
        success: function (result) {
            if (result.status === 200) {
                success(result.msg);
            }
        }
    })
}


$('#production-hours-form').form({
    inline: true,
    keyboardShortcuts: false,
    on: "submit",
    fields: {
        workOrderNumber: {
            identifier: "workOrderNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "请选择工单"
                }
            ]
        },
        productCode: {
            identifier: "productCode",
            rules: [
                {
                    type: "empty",
                    prompt: "产品编码不能为空"
                }
            ]
        },
        productNumber: {
            identifier: "productNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "产品型号不能为空"
                }
            ]
        },
        productionQuantity: {
            identifier: "productionQuantity",
            rules: [
                {
                    type: "empty",
                    prompt: "生产数量不能为空"
                }
            ]
        },
        recordDate: {
            identifier: "recordDate",
            rules: [
                {
                    type: "empty",
                    prompt: "请选择日期"
                }
            ]
        },
        actualOutput: {
            identifier: "actualOutput",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写实际产出数量"
                },
                {
                    type: 'regExp',
                    value: /^\d+$/,
                    prompt: "非法填写"
                }
            ]
        },
        workingHours: {
            identifier: "workingHours",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写正班工时"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        overTime: {
            identifier: "overTime",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写加班工时"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        totalWorkingHours: {
            identifier: "totalWorkingHours",
            rules: [
                {
                    type: "empty",
                    prompt: "总工时不能为空"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        remarks: {
            identifier: "remarks",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写备注 例(无)"
                }
            ]
        }
    },
    onSuccess: function () {
        save_production_hours();
    }
});

function save_production_hours() {
    $.post({
        url: '/plan/saveProductionTimeInformation',
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "workOrderNumber": $('#workOrderNumber').val(),
            "productCode": $('#productCode').val(),
            "productNumber": $('#productNumber').val(),
            "productionQuantity": parseInt($('#productionQuantity').val()),
            "actualOutput": parseInt($('#actualOutput').val()),
            "workingHours": parseFloat($('#workingHours').val()),
            "overTime": parseFloat($('#overTime').val()),
            "totalWorkingHours": parseFloat($('#totalWorkingHours').val()),
            "remarks": $('#remarks').val().trim(),
            "recordDate": $('#recordDate').val()
        }),
        success: function (result) {
            if (result.status === 200) {
                clear_re1();
                success(result.msg);
            }
        }
    })
}

$('#no-production-hours-form').form({
    inline: true,
    keyboardShortcuts: false,
    on: "submit",
    fields: {
        projectNumber: {
            identifier: "projectNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写项目号"
                },
                {
                    type: "regExp",
                    value: /^([0-9a-zA-Z]+)$/,
                    prompt: "非法项目号"
                }
            ]
        },
        projectName: {
            identifier: "projectName",
            rules: [
                {
                    type: 'empty',
                    prompt: "请填写项目名称"
                }
            ]
        },
        date: {
            identifier: "date",
            rules: [
                {
                    type: "empty",
                    prompt: "请选择日期"
                }
            ]
        },
        numberOfOperations: {
            identifier: "numberOfOperations",
            rules: [
                {
                    type: 'empty',
                    prompt: "请填写作业人数"
                },
                {
                    type: "regExp",
                    value: /^(0|[1-9][0-9]*)$/,
                    prompt: "请正确填写人数"
                }
            ]
        },
        noWorkingTime: {
            identifier: "noWorkingTime",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写正班工时"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        noOverTime: {
            identifier: "noOverTime",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写加班工时"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        noTotalWorkingTime: {
            identifier: "noTotalWorkingTime",
            rules: [
                {
                    type: "empty",
                    prompt: "总计工时不能为空"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法数据"
                }
            ]
        },
        remarks: {
            identifier: "no-remarks",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写备注 例(无)"
                }
            ]
        }
    },
    onSuccess: function () {
        save_project();
    }
});

function save_project() {
    $.post({
        url: '/plan/saveProjectInfo',
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "projectNumber": $('#projectNumber').val().trim(),
            "projectName": $('#projectName').val().trim(),
            "projectDate": $('#date').val(),
            "numberOfOperations": $('#numberOfOperations').val(),
            "workingHours": parseFloat($('#noWorkingTime').val()),
            "overTime": parseFloat($('#noOverTime').val()),
            "totalWorkingTime": parseFloat($('#noTotalWorkingTime').val()),
            "remarks": $('#no-remarks').val().trim()
        }),
        success: function (result) {
            if (result.status === 200) {
                $('#no-production-hours-form').form('clear');
                success(result.msg);
            }
        }
    })
}

$('#production-management-hours-form').form({
    inline: true,
    keyboardShortcuts: false,
    on: "submit",
    fields: {
        name: {
            identifier: "name",
            rules: [
                {
                    type: "empty",
                    prompt: "姓名不能为空"
                }
            ]
        },
        jobNumber: {
            identifier: "jobNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "工号不能为空"
                }
            ]
        },
        position: {
            identifier: "position",
            rules: [
                {
                    type: "empty",
                    prompt: "职位不能为空"
                }
            ]
        },
        projectNumber: {
            identifier: "m-projectNumber",
            rules: [
                {
                    type: "empty",
                    prompt: "请选择项目号"
                }
            ]
        },
        projectName: {
            identifier: "m-projectName",
            rules: [
                {
                    type: "empty",
                    prompt: "项目名称不能为空"
                }
            ]
        },
        manageDate: {
            identifier: "manage-date",
            rules: [
                {
                    type: "empty",
                    prompt: "日期不能为空"
                }
            ]
        },
        workingHours: {
            identifier: "m-noWorkingTime",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写正班工时"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        overTime: {
            identifier: "m-noOverTime",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写加班工时"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法填写"
                }
            ]
        },
        totalWorkingTime: {
            identifier: "m-noTotalWorkingTime",
            rules: [
                {
                    type: "empty",
                    prompt: "总计工时不能为空"
                },
                {
                    type: 'regExp',
                    value: /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/,
                    prompt: "非法数据"
                }
            ]
        },
        remarks: {
            identifier: "no-m-remarks",
            rules: [
                {
                    type: "empty",
                    prompt: "请填写备注 例(无)"
                }
            ]
        }
    },
    onSuccess: function () {
        save_att_time();
    }
});

function save_att_time() {
    $.post({
        url: '/plan/saveAttTimeInfo',
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "name": $('#name').val(),
            "jobNumber": $('#jobNumber').val(),
            "position": $('#position').val(),
            "recordDate": $('#manage-date').val(),
            "projectNumber": $('#m-projectNumber').val(),
            "projectName": $('#m-projectName').val(),
            "workingTime": parseFloat($('#m-noWorkingTime').val()),
            "overTime": parseFloat($('#m-noOverTime').val()),
            "totalWorkingTime": parseFloat($('#m-noTotalWorkingTime').val()),
            "remarks": $('#no-m-remarks').val().trim()
        }),
        success: function (result) {
            if (result.status === 200) {
                clear_re3();
                success(result.msg);
            }
        }
    })
}

let pre_div = $('#view1');
$('.stat-working').click(function () {
    get_production_working_hours(start, pageSize);
    select($('#view1'));
});

$('#reload-product-hours').click(function () {
    get_production_working_hours(start, pageSize);
});

$('.stat-no-working').click(function () {
    select($('#view2'));
    get_project_working_hours_info(start, pageSize)
    $('#top-menu').hide();
});

$('#reload-project-hours').click(function () {
    get_project_working_hours_info(start, pageSize);
});


$('.stat-management').click(function () {
    get_manage_working_hours_info(start, pageSize);
    get_all_project_number();
    select($('#view3'));
    $('#top-menu').hide();
});
$('.fo-lr').click(function () {
    select($('#f-one'));
    $('#top-menu').hide();
})

$('.ft-lr').click(function () {
    select($('#f-two'));
    get_all_project_number();
    $('#top-menu').hide();
})

$('.fte-lr').click(function () {
    select($('#f-three'));
    get_all_project_number();
    get_user_part_info();
    $('#top-menu').hide();
})

$('.hours-stat').click(function () {
    select($('#stat1'));
    default_stat();
    $('#top-menu').hide();
});

$('.project-hours-stat').click(function () {
    select($('#stat2'));
    default_project_Stat();
    $('#top-menu').hide();
})

$('.del-project').click(function () {
    select($('#del1'));
    $('#top-menu').hide();
});

$('.add-project').click(function () {
    select($('#add1'));
    $('#top-menu').hide();
})

$('.manage-hours-stat').click(function () {
    select($('#stat3'));
    default_manage_Stat();
    $('#top-menu').hide();
})

$('.show-menu').click(function () {
    $(this).text("隐藏顶部菜单");
    $(this).attr('data-tooltip', "双击隐藏顶部菜单");
    $(this).attr('id', "hide-menu");
    $('#head').show();
})


$('#reload-manage-hours').click(function () {
    get_manage_working_hours_info(start, pageSize);
})


function select(target) {
    pre_div.hide(200);
    target.show(200);
    pre_div = target;
}


function actual_calculation(agr1, arg2, target) {
    let curTime = agr1.val();
    let otherTime = arg2.val();
    if (verify_time(curTime) && verify_time(otherTime)) {
        let total = parseFloat(curTime) + parseFloat(otherTime);
        target.val(total);
    }
}


$('#workingHours').blur(function () {
    actual_calculation($(this), $('#overTime'), $('#totalWorkingHours'))
});

$('#overTime').blur(function () {
    actual_calculation($('#workingHours'), $(this), $('#totalWorkingHours'))
});
$('#noOverTime').blur(function () {
    actual_calculation($('#noWorkingTime'), $(this), $('#noTotalWorkingTime'))
});
$('#noWorkingTime').blur(function () {
    actual_calculation($(this), $('#noOverTime'), $('#noTotalWorkingTime'))
});

$('#m-noOverTime').blur(function () {
    actual_calculation($('#m-noWorkingTime'), $(this), $('#m-noTotalWorkingTime'))
});
$('#m-noWorkingTime').blur(function () {
    actual_calculation($(this), $('#m-noOverTime'), $('#m-noTotalWorkingTime'))
});

function canv_pie(target, data) {
    let myChart = echarts.init(target);
    let option;
    option = {
        title: {
            text: data.title
        },
        tooltip: {
            "textStyle": {
                "fontSize": 16
            },
            trigger: 'axis'
        },
        legend: {
            data: ['正班工时', '加班工时', '总计工时']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: data.timeList
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '正班工时',
                type: 'line',
                stack: 'Total',
                data: data.workingHoursList
            },
            {
                name: '加班工时',
                type: 'line',
                stack: 'Total',
                data: data.overTimeList
            },
            {
                name: '总计工时',
                type: 'line',
                stack: 'Total',
                data: data.totalHoursList
            }
        ]
    };
    myChart.setOption(option);
}

$('#month-stat').click(function () {
    let idf = $('#switch').attr('idf');
    switch (idf) {
        case "0":
            stat_production_by_month();
            break;
        case "1":
            stat_by_month_number();
            break;
    }
});

$('#year-stat').click(function () {
    let idf = $('#switch').attr('idf');

    switch (idf) {
        case "0":
            stat_production_by_year();
            break;
        case "1":
            stat_by_year_number()
            break;
    }
});

$('#project-month-stat').click(function () {
    let idf = $('#project-switch').attr('idf');

    switch (idf) {
        case "0":
            stat_project_by_month();
            break;
        case "1":
            stat_by_month_project_number();
            break;
    }
});
$('#project-year-stat').click(function () {
    let idf = $('#project-switch').attr('idf');

    switch (idf) {
        case "0":
            stat_project_by_year();
            break;
        case "1":
            stat_by_year_project_number();
            break;
    }
});

$('#manage-year-stat').click(function () {
    let idf = $('#manage-switch').attr('idf');
    let flag = $('#all-switch').attr('idf');
    switch (idf) {
        case "0":
            stat_manage_by_year(flag);
            break;
        case "1":
            stat_manage_by_year_project_number(flag);
            break;
    }
});

$('#manage-month-stat').click(function () {
    let idf = $('#manage-switch').attr('idf');
    let flag = $('#all-switch').attr('idf');
    switch (idf) {
        case "0":
            stat_manage_by_month(flag);
            break;
        case "1":
            stat_manage_by_month_project_number(flag);
            break;
    }
});

$('#all-report-year').click(function () {
    let url = "/plan/allThePeopleManagementTimeReport/"
    let date = new Date();
    let fileName = "所有人全年管理工时记录-(" + date.getFullYear() + ")" + date.getTime();
    report(url, 0, fileName);
});

$('#all-report-month').click(function () {
    let url = "/plan/allThePeopleManagementTimeReport/"
    let date = new Date();
    // let month = date.getMonth();
    let fileName = "所有人当月管理工时记录-(" + date.getFullYear();
    report(url, 1, fileName);
});

$('#personal-report-year').click(function () {
    let url = "/plan/personManagementTimeReport/"
    let date = new Date();
    let fileName = "个人全年管理工时记录-(" + date.getFullYear() + ")" + date.getTime();
    report(url, 0, fileName);
});

$('#personal-report-month').click(function () {
    let url = "/plan/personManagementTimeReport/"
    let date = new Date();
    // let month = date.getMonth() + 1;
    let fileName = "个人当月管理工时记录-(" + date.getFullYear();
    report(url, 1, fileName);
});


function report(url, idf, fileName) {
    let time = $('#manage-stat-time').val();
    if (time.length === 0) {
        failure("请选择时间")
        return;
    }
    let split = time.split("-");
    let year = split[0];
    let month = split[1];
    if (idf === 1) {
        fileName += "-" + month + ")" + new Date().getTime();
    }
    url = url + idf + "/" + year + "/" + month;
    download(fileName, url);
}


function default_stat() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    $('#stat-time').val(year + "-" + month);
    let url = "/plan/getProductHoursByMonth/" + year + "/" + month
    get_stat_data(url, "cv1");
}

function default_project_Stat() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    $('#project-stat-time').val(year + "-" + month);
    let url = "/plan/monthlyStatisticsOfProjectHours/" + year + "/" + month
    get_stat_data(url, "cv2");
}

function default_manage_Stat() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    $('#manage-stat-time').val(year + "-" + month);
    let idf = $('#all-switch').attr('idf');
    let url = "/plan/manageTimeStatisticsByMonth/" + idf + "/" + year + "/" + month;
    get_stat_data(url, "cv3");
}


function stat_by_year(time, prefix, idf) {
    if (time.length === 0) {
        failure("请选择时间")
        return;
    }
    let split = time.split("-");
    let year = split[0];
    let url = prefix + year;
    let id;
    switch (idf) {
        case 1:
            id = "cv1";
            //get_stat_data(url,"cv1");
            break;
        case 2:
            id = "cv2";
            //get_stat_project_data(url,"cv2");
            break;
        case 3:
            id = "cv3";
            //get_stat_manage_data(url,"cv3");
            break;
    }
    get_stat_data(url, id);
}

function stat_production_by_year() {
    stat_by_year($('#stat-time').val(), "/plan/getProductHoursByYear/", 1)
}

function stat_project_by_year() {
    stat_by_year($('#project-stat-time').val(), "/plan/yearStatisticsOfProjectHours/", 2)
}

function stat_project_by_month() {
    stat_by_month($('#project-stat-time').val(), "/plan/monthlyStatisticsOfProjectHours/", 2);
}

function stat_production_by_month() {
    stat_by_month($('#stat-time').val(), "/plan/getProductHoursByMonth/", 1)
}

function stat_manage_by_month(flag) {
    stat_by_month($('#manage-stat-time').val(), "/plan/manageTimeStatisticsByMonth/" + flag + "/", 3)
}

function stat_manage_by_year(idf) {
    stat_by_year($('#manage-stat-time').val(), "/plan/manageTimeStatisticsByYear/" + idf + "/", 3)
}

function stat_by_month(time, prefix, idf) {
    if (time.length === 0) {
        failure("请选择时间")
        return;
    }
    let split = time.split("-");
    let year = split[0];
    let month = split[1];
    let url = prefix + year + "/" + month
    let id;
    switch (idf) {
        case 1:
            id = "cv1";
            //get_stat_data(url,"cv1");
            break;
        case 2:
            id = "cv2";
            //get_stat_project_data(url,"cv2");
            break;
        case 3:
            id = "cv3"
            //get_stat_manage_data(url,"cv3");
            break;
    }
    get_stat_data(url, id);
}


function stat_by_year_number() {
    let workOrderNumber = $('#stat-workOrderNumber').val();
    if (verify_work_order_number(workOrderNumber)) {
        failure("不合法工单")
        return;
    }
    let url = "/plan/getYearProductHoursByNumber/" + workOrderNumber + "/"
    stat_by_year($('#stat-time').val(), url, 1);
}

function stat_by_month_number() {
    let workOrderNumber = $('#stat-workOrderNumber').val();
    if (verify_work_order_number(workOrderNumber)) {
        failure("不合法工单")
        return;
    }
    let url = "/plan/getMonthProductHoursByNumber/" + workOrderNumber + "/"
    stat_by_month($('#stat-time').val(), url, 1);
}

function stat_by_year_project_number() {
    let projectNumber = $('#project-projectNumber').val();
    if (projectNumber.length === 0) {
        failure("请选择项目编号")
        return;
    }
    let url = "/plan/projectNumberYearStatisticsOfProjectHours/" + projectNumber + "/"
    stat_by_year($('#project-stat-time').val(), url, 2);
}

function stat_by_month_project_number() {
    let projectNumber = $('#project-projectNumber').val();
    if (projectNumber.length === 0) {
        failure("请选择项目编号")
        return;
    }
    let url = "/plan/projectNumberMonthStatisticsOfProjectHours/" + projectNumber + "/"
    stat_by_month($('#project-stat-time').val(), url, 2);
}

function stat_manage_by_month_project_number(flag) {
    let projectNumber = $('#manage-projectNumber').val();
    if (projectNumber.length === 0) {
        failure("请选择项目编号")
        return;
    }
    let url = "/plan/monthlyManagementTimeStatisticsByNumber/" + flag + "/" + projectNumber + "/"
    stat_by_month($('#manage-stat-time').val(), url, 3);
}

function stat_manage_by_year_project_number(idf) {
    let projectNumber = $('#manage-projectNumber').val();
    if (projectNumber.length === 0) {
        failure("请选择项目编号")
        return;
    }
    let url = "/plan/yearManagementTimeStatisticsByNumber/" + idf + "/" + projectNumber + "/"
    stat_by_year($('#manage-stat-time').val(), url, 3);
}

function get_stat_data(url, id) {
    $.get({
        url: url,
        success: function (result) {
            $('#head').hide();
            $('#top-menu').show();
            canv_pie(document.getElementById(id), result.data)
        }
    })
}

function get_stat_project_data(url) {
    $.get({
        url: url,
        success: function (result) {
            $('#top-menu').show();
            $('#head').hide();
            canv_pie(document.getElementById("cv2"), result.data)
        }
    })
}

function get_stat_manage_data(url) {
    $.get({
        url: url,
        success: function (result) {
            $('#head').hide();
            $('#top-menu').show();
            canv_pie(document.getElementById("cv3"), result.data)
        }
    })
}

$('#switch').click(function () {
    switch_c($(this), $('#switch-icon'), $('#wmc'));
})

$('#project-switch').click(function () {
    switch_c($(this), $('#project-switch-icon'), $('#project-wmc'));
})

$('#manage-switch').click(function () {
    switch_c($(this), $('#manage-switch-icon'), $('#manage-wmc'));
})

$('#all-switch').click(function () {
    $('#all-switch-icon').toggleClass('off').toggleClass('on').toggleClass('switch-active');
    let idf = $(this).attr('idf');
    if (idf === "0") {
        $(this).attr('idf', 1);
    } else {
        $(this).attr('idf', 0);
    }
})

function switch_c(t1, t2, t3) {
    t2.toggleClass('off').toggleClass('on').toggleClass('switch-active');
    let idf = t1.attr('idf');
    if (idf === "0") {
        t3.show();
        t1.attr("idf", 1)
    } else {
        t3.hide();
        t1.attr("idf", 0)
    }
}

$('#year-productHours-export').click(function () {
    production_down(2, $('#product-hours-report-time').val(), "生产工时年报(");
})

$('#month-productHours-export').click(function () {

    production_down(2, $('#product-hours-report-time').val(), "生产工时月报(");
})


function production_down(idf, time, title) {
    let url;
    let split = time.split("-");
    let year = split[0];
    let month = split[1];
    switch (idf) {
        case 1:
            url = "/plan/exportOfAnnualProductionHours/" + year;
            break;
        case 2:
            url = "/plan/exportOfMonthlyProductionHours/" + year + "/" + month;
            break;
        default:
            failure("非法操作");
            return;
    }

    let fileName = title + year + "-" + month + ")-" + new Date().getTime();
    download(fileName, url)
}


$('#year-projectHours-export').click(function () {
    project_down(1, $('#project-hours-report-time').val(), "项目工时年报(")
})

$('#month-projectHours-export').click(function () {
    project_down(2, $('#project-hours-report-time').val(), "项目工时月报(")
})

function project_down(idf, time, title) {
    let url;
    let split = time.split("-");
    let year = split[0];
    let month = split[1];
    switch (idf) {
        case 1:
            url = "/plan/exportOfAnnualProjectHours/" + year;
            break;
        case 2:
            url = "/plan/exportOfMonthlyProjectHours/" + year + "/" + month;
            break;
        default:
            failure("非法操作");
            return;
    }

    let fileName = title + year + "-" + month + ")-" + new Date().getTime();
    download(fileName, url)
}

$('#year-manageHours-export').click(function () {
    manage_down(1, $('#manage-hours-report-time').val(), "个人管理工时年报(")
})

$('#month-manageHours-export').click(function () {
    manage_down(2, $('#manage-hours-report-time').val(), "个人管理工时月报(")
})

function manage_down(idf, time, title) {
    let jobNumber = $('#other-jobNumber').val();
    let name = jobNumber;
    if (jobNumber.length === 0) {
        jobNumber = "EDG";
        name = "我的";
    }
    let url;
    let split = time.split("-");
    let year = split[0];
    let month = split[1];
    switch (idf) {
        case 1:
            url = "/plan/exportPersonalOfAnnualManagementHours/" + jobNumber + "/" + year;
            break;
        case 2:
            url = "/plan/exportPersonalOfMonthlyManagementHours/" + jobNumber + "/" + year + "/" + month;
            break;
        default:
            failure("非法操作");
            return;
    }
    let fileName = name + title + year + "-" + month + ")-" + new Date().getTime();
    download(fileName, url)
}


$('#query-other').dblclick(function () {
    let target = $(this);
    $.get({
        url: '/admin/getAllJobNumber',
        success: function (result) {
            if (result.status === 200) {
                package_menu_plus(result.data, $('.m-jobNumber-menu'));
                $('#jobNumber-se1').show();
                target.attr("id", "pack-up");
                target.removeAttr("data-tooltip");
                target.html("收起");
                other = true;
            }
        }
    })
})

$(document).on('click', '#pack-up', function () {
    $(this).attr("id", "query-other");
    $(this).attr("data-tooltip", "请双击");
    $('#jobNumber-se1').hide();
    $('#other-jobNumber').val("");
    $(this).html("查询其他人");
    other = false;
})

$(document).on('dblclick', '#hide-menu', function () {
    let tar = $('.show-menu');
    $(this).attr("id", "top-menu");
    tar.attr("data-tooltip", "显示顶部菜单!");
    $('#head').hide();
    tar.text("显示顶部菜单");
});

function get_cur_month_mwh(pageNum, jobNumber, time) {
    $.get({
        url: '/plan/getCurMonthManageWorkingHoursInfo/' + time + "/" + pageNum + "/" + jobNumber,
        success: function (result) {
            if (result.status === 200) {
                page_management_hours_info(result.data.list, $('#view3-table tbody'));
                renderTfoot("\"" + jobNumber + "\"", result.data, 'get_cur_month_mwh', time, pageNum);
            }
        }
    })
}

function getTime() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    return year + "-" + month;
}
